package com.hyt.it.ogt.kq.main.controller.bm;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.httpclient.FormFieldKeyValuePair;
import com.hyt.core.util.UUIDUtils;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.utils.KqStringUtils;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectSubject;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentSubject;
import com.hyt.it.ogt.kq.service.bm.model.vo.SubjectStatVo;
import com.hyt.it.ogt.kq.service.bm.service.IProjectService;
import com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentSubjectService;
import com.hyt.log.annotation.Log;
import com.hyt.swagger.ApiVersion;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 报名项目科目 前端控制器
 * </p>
 *
 * @author wangt
 * @since 2020-05-15
 */
@RestController
@Api(tags = "7.报名项目科目", value = "报名项目科目")
@ApiSort(value = 7)
@RequestMapping("/bm/project-subject")
public class ProjectSubjectController extends BaseController {

    @Autowired
    private IProjectSubjectService iProjectSubjectService;
    @Autowired
    private IStudentSubjectService iStudentSubjectService;
    @Autowired
    private IProjectService iProjectService;


    @Deprecated
    @ApiOperation(value = "6.1.1 添加报名项目科目", notes = "20000:成功；31061103：添加报名项目科目数据失败")
    @ApiOperationSort(value = 1)
    @Log(module = "报名项目科目管理", business = "添加报名项目科目")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody ProjectSubject projectSubject) {
        if (KqStringUtils.isBlank(projectSubject.getSubjectId())) {
            projectSubject.setSubjectId(UUIDUtils.newSortUUID());
        }
        try {
            if (projectSubject.getPrice() != null && projectSubject.getPrice().doubleValue() > ConfigManager.MAXPRICE) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061111).build();
            }
            List<ProjectSubject> list = iProjectSubjectService.querySubjectList(projectSubject.getProjectId(), projectSubject.getSubjectName());
            if (CollUtil.isNotEmpty(list)) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061102).build();
            }
            String subjectId = UUIDUtils.newSortUUID();
            projectSubject.setId(subjectId);
            iProjectSubjectService.save(projectSubject);
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(projectSubject).build();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061103).data(projectSubject).build();
        }
    }

    @Deprecated
    @ApiOperation(value = "6.1.2 编辑报名项目科目数据", notes = "20000:成功；31061211：编辑报名项目科目数据失败")
    @ApiOperationSort(value = 2)
    @Log(module = "报名项目科目管理", business = "编辑报名项目科目数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Object update(@RequestBody ProjectSubject projectSubject) {
        try {
            if (projectSubject.getPrice() != null && projectSubject.getPrice().doubleValue() > ConfigManager.MAXPRICE) {
                return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061111).build();
            }
            return iProjectSubjectService.update(projectSubject);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061211).data(projectSubject).build();
        }
    }

    @ApiOperation(value = "6.1.3 查看报名项目科目数据", notes = "20000:成功；31061301：未查找到报名项目科目数据")
    @ApiOperationSort(value = 3)
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> query(@PathVariable String id) {
        ProjectSubject projectSubject = iProjectSubjectService.getById(id);
        if (projectSubject != null) {
            if (null == projectSubject.getPrice()) {
                projectSubject.setPrice(BigDecimal.ZERO);
            }
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(projectSubject).build();
        } else {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061301).data(projectSubject).build();
        }
    }

    @ApiOperation(value = "6.1.4 统计报名项目科目数据", notes = "20000:成功；31061301：未查找到报名项目科目数据")
    @ApiOperationSort(value = 4)
    @RequestMapping(value = "/statSubjectByProjectId", method = RequestMethod.GET)
    public ApiResponse<Object> statSubjectByProjectId(String projectId, String subjectId) {
        List<String> permissionList = getPermissionList(projectId);
        String deptId = "";
        Project project = iProjectService.getById(projectId);
        if (0 == project.getIsOpen()) {
            deptId = getDeptId();
        }
        List<SubjectStatVo> subjectList = iProjectSubjectService.statSubjectByProjectId(projectId, permissionList, subjectId, deptId);
        if (subjectList.size() > 0) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(subjectList.get(0)).build();
        } else {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061301).data(subjectList).build();
        }
    }

    @ApiOperation(value = "6.1.5 删除报名科目数据", notes = "20000:成功；31061502：删除科目异常")
    @ApiOperationSort(value = 5)
    @ThrowsAdvice(code = 31061502, print = "删除报名科目数据异常")
    @Log(module = "报名项目科目管理", business = "删除报名科目数据")
    @RequestMapping(value = "/deleteSubject", method = RequestMethod.DELETE)
    public ApiResponse<Object> deleteSubject(String id, String projectId) throws Exception {
        ProjectSubject projectSubject = iProjectSubjectService.querySubject(id);
        List<StudentSubject> list = iStudentSubjectService.queryStudentSubject(null, null, projectSubject.getSubjectId(), null, new ArrayList<String>(), projectId);
        if (list != null && list.size() > 0) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_31061501).build();
        }
        iProjectSubjectService.deleteSubject(id, projectId, projectSubject.getSubjectId());
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
    }

    @ApiOperation(value = "6.2.1 获取报名的科目列表", notes = "20000:成功；")
    @ApiOperationSort(value = 6)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ThrowsAdvice(code = 31062101, print = "获取报名的科目列表失败")
    @Log(module = "报名项目科目管理", business = "获取报名的科目列表")
    @GetMapping(value = "/selectSubject")
    public ApiResponse<List<FormFieldKeyValuePair>> selectSubject(@RequestParam String projectId) {
        return ApiResponse.<List<FormFieldKeyValuePair>>builder()
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .data(iProjectSubjectService.selectSubject(projectId))
                .build();
    }
}
